<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/* Check form token
 * 
 * @access	public
 * @param	string	submitted token
 * @param	integer	expiration time - default 5 minutes
 * @return	bool	token valid?
 * 
 */
function check_token($submitted_token, $expire = 300)
{
	$CI =& get_instance();
	
	$token_age = time() - $CI->session->userdata('token_time');
	$token = $CI->session->userdata('token');

	// 5 minutes to submit the form
	if ($token_age <= $expire)
	{
		if ($token === $submitted_token)
		{
			return TRUE;
		}		
	}
	
	// Expired or invalid
	$CI->session->unset_userdata('token');
	return FALSE;
}
/**
 * Enter description here...
 *
 */
function generate_token()
{
	$CI =& get_instance();
	
	$token = md5(uniqid(rand(), TRUE));
	$CI->session->set_userdata('token', $token);
	$CI->session->set_userdata('token_time', time());
	
	return $token;
}
function hello()
{
	echo "hello world!";
}
function array_remove_item_id($arrs,$id)
{
	if($arrs == null)
		return null;
	$result = array();
	foreach($arrs as $key=>$value)
	{
		if($id!= $value['id'])
		{
			$result[$key]= $value;
		}
	}
	return $result;
}
function array_remove_item($arrs, $index)
{
	if($arrs == null)
		return null;
	$result = array();
	foreach($arrs as $key=>$value)
	{
		if($key!= $index)
		{
			$result[$key]= $value;
		}
	}
	return $result;
}
/**
 * Make combobox for array of category
 * root
 *   -Category 1
 *    	- Category 11
 *    	- Category 12
 *    	- Category 13
 *   -Category 2
 *    	- Category 21
 *    	- Category 22
 *    	- Category 23
 *   -Category 3
 *    	- Category 31
 *    	- Category 32
 *    	- Category 33
 *
 * @param unknown_type $arr
 *         ['name'] = '' string
 *         ['id'] = '' string
 *         ['class'] = '' string
 *         ['value']=array
 *         
 */
function form_categories_combobox($arr,$selected='',$root='Root')
{
	$html = "<select id='". $arr['id'] ."' name='". $arr['name']  ."' >";
	if($root != '')
	{
		$html = $html . "<option value='0'>". $root . "</option>";
	}
	$categories = $arr['value'];
	$tree = array();
	$arr_temps = array();
	/* make array temp*/
	foreach($categories as $category)
	{
		$arr_temps[$category['id']] = $category;
	}
	for($i = 0; $i < count($categories);)
	{
		
		foreach($arr_temps as $temp)
		{
			
			if($temp['parent_id']==0)
			{
				$tree[$temp['id']]=array('id'=>$temp['id'],'title'=>$temp['title'],'level'=>0);
				$i++;
				array_remove_item($arr_temps,$temp['id']);
			}
		
			else
			{
				if($tree[$temp['parent_id']] != null)
				{
					$parent_level = $tree[$temp['parent_id']]['level'];
					$level = $parent_level + 1;
					$tree[$temp['id']]=array('id'=>$temp['id'],'title'=>$temp['title'],'level'=>$level);
					$i++;
					array_remove_item($arr_temps,$temp['id']);
				}
			}
		}
	}
	
	foreach($tree as $item)
	{
		$items[$item['id']]['id'] = $item['id'];
		$pre = '';
		for($i =0; $i< $item['level'];$i++)
		{
			$pre = $pre."-";
		}
		$items[$item['id']]['title'] = $pre . $item['title'];
	}
	
	if($items != null)
	{ 
		foreach($items as $item)
		{  
			if($selected != $item['id'])
			{
				$html = $html . "<option value='". $item['id'] ."'>". $item['title'] ."</option>";
			}
			else
			{
				$html = $html . "<option selected='yes' value='". $item['id'] ."'>". $item['title'] ."</option>";
			}
		}
	}
	$html = $html . '</select>';
	return $html;
}
/* End of file MY_form_helper.php */
/* Location: ./application/helpers/MY_form_helper.php */